//
//  MainTableViewController.swift
//  MicroBlog
//
//  Created by Apple on 16/9/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
class MainTableViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let TabBar = HMTabBar()
        //调用闭包
        TabBar.composeButtonClosoure = { [weak self] in
            print("我是闭包调用的")
            if !HMUserAccountViewModel.sharedUserAccountViewModel.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录!亲~")
                return
            }
            if let target = self {
                //已经登录
                let composeView = HMComposeView()
                composeView.show(target: target)
            }
        }
        //指定代理
        TabBar.tabBarDelegate = self
        setValue(TabBar, forKey: "tabBar")
        //设置tintColor
        UITabBar.appearance().tintColor = UIColor.orange
        //添加vc
        addChildViewController(childController: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: ProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
    }
    
    //添加子控制器方法的重载
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        //设置图片和文字
        childController.tabBarItem.image = UIImage(named: imageName)
        //修改图片的渲染模式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange ], for: .selected)
        //设置标题
        childController.title = title
        //创建导航控制器
        let nav = HMNaviViewController(rootViewController: childController)
        //添加vc
        addChildViewController(nav)
    }
    
}

//延展执行代理
extension MainTableViewController: HMTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理调用的")
    }
}
